package cn.stylefeng.guns.modular.ctm.service.impl;

import cn.stylefeng.guns.modular.api.enums.WorkEnum;
import cn.stylefeng.guns.modular.api.enums.WorkTownEnum;
import cn.stylefeng.guns.modular.ctm.dto.CtmDTO;
import cn.stylefeng.guns.modular.ctm.entity.Ctm;
import cn.stylefeng.guns.modular.ctm.mapper.CtmMapper;
import cn.stylefeng.guns.modular.ctm.service.CtmService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 城市信息 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2022-05-24
 */
@Service
public class CtmServiceImpl extends ServiceImpl<CtmMapper, Ctm> implements CtmService {

    @Autowired
    CtmMapper ctmMapper;

    @Override
    public IPage<CtmDTO> selectByPage(Page page, Ctm ctm) {
        //todo 获取在线用户信息
        String userId = "77d6d66479cb80fd1b70736a36a65912";
        ctm.setOnlineUserId(userId);
        return ctmMapper.selectByPage(page, ctm);
    }

    @Override
    public CtmDTO selectInfoById(String inputJobId) {
        //todo 获取在线用户信息
        String userId = "77d6d66479cb80fd1b70736a36a65912";
        return ctmMapper.selectInfoById(inputJobId, userId);
    }

    @Override
    public List<Map<String, Object>> workList() {
        Map<String, List<WorkEnum>> groupByCode = Arrays.stream(WorkEnum.values()).collect(Collectors.groupingBy(WorkEnum::getPCode));
        return groupByCode.values().stream()
                .map(workEnums -> {
                    List<Map<String, Object>> childList = workEnums.stream()
                            .map(workEnum -> {
                                Map<String, Object> map = new HashMap<>();
                                map.put("id", workEnum.getCode());
                                map.put("name", workEnum.getValue());
                                return map;
                            })
                            .collect(Collectors.toList());
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", workEnums.get(0).getPCode());
                    map.put("name", workEnums.get(0).getPValue());
                    map.put("child", childList);
                    return map;
                }).sorted(Comparator.comparing(map -> map.get("id").toString())).collect(Collectors.toList());
    }

    @Override
    public List<Map<String, Object>> countyList() {
        Map<String, List<WorkTownEnum>> groupByCode = Arrays.stream(WorkTownEnum.values()).collect(Collectors.groupingBy(WorkTownEnum::getCityCode));
        return groupByCode.values().stream()
                .map(workTownEnums -> {
                    List<Map<String, Object>> childList = workTownEnums.stream()
                            .map(workTownEnum -> {
                                Map<String, Object> map = new HashMap<>();
                                map.put("id", workTownEnum.getCode());
                                map.put("name", workTownEnum.getValue());
                                return map;
                            })
                            .collect(Collectors.toList());
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", workTownEnums.get(0).getCityCode());
                    map.put("name", workTownEnums.get(0).getCityName());
                    map.put("child", childList);
                    return map;
                }).sorted(Comparator.comparing(map -> map.get("id").toString())).collect(Collectors.toList());
    }

    @Override
    public List<Map<String, Object>> hotJob() {
        List<Map<String, Object>> result = new ArrayList<>();
        List<Map<String, Object>> strings = ctmMapper.hotJob();
        for (int i = 0; i < strings.size(); i++) {
            String work = strings.get(i).get("work").toString();
            WorkEnum byCode = WorkEnum.getByCode(work);
            if (ObjectUtils.isEmpty(byCode)) {
                WorkEnum byPCode = WorkEnum.getByPCode(work);
                if (!ObjectUtils.isEmpty(byPCode)) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", byPCode.getPCode());
                    map.put("name", byPCode.getPValue());
                    result.add(map);
                }
            } else {
                Map<String, Object> map = new HashMap<>();
                map.put("id", byCode.getCode());
                map.put("name", byCode.getValue());
                result.add(map);
            }
        }
        return result;
    }

    @Override
    public void collectionPost(String inputJobId) {
        //todo 获取在线用户信息
        String userId = "77d6d66479cb80fd1b70736a36a65912";
        int count = ctmMapper.countCollectPost(inputJobId, userId);
        if (count < 1) {
            ctmMapper.collectionPost(inputJobId, userId);
        }
    }

    @Override
    public void uncollectPost(String inputJobId) {
        //todo 获取在线用户信息
        String userId = "77d6d66479cb80fd1b70736a36a65912";
        ctmMapper.uncollectPost(inputJobId, userId);
    }

    @Override
    public IPage<CtmDTO> collectionPostPage(Page page, Ctm ctm) {
        //todo 获取在线用户信息
        String userId = "77d6d66479cb80fd1b70736a36a65912";
        ctm.setOnlineUserId(userId);
        return ctmMapper.collectionPostPage(page, ctm);
    }

    @Override
    public void notInterested(String inputJobId) {
        //todo 获取在线用户信息
        String userId = "77d6d66479cb80fd1b70736a36a65912";
        int count = ctmMapper.countNotInterested(inputJobId, userId);
        if (count < 1) {
            ctmMapper.notInterested(inputJobId, userId);
        }
    }

    @Override
    public void cancelNotInterested(String inputJobId) {
        //todo 获取在线用户信息
        String userId = "77d6d66479cb80fd1b70736a36a65912";
        ctmMapper.cancelNotInterested(inputJobId, userId);
    }

    @Override
    public IPage<CtmDTO> notInterestedPage(Page page, Ctm ctm) {
        //todo 获取在线用户信息
        String userId = "77d6d66479cb80fd1b70736a36a65912";
        ctm.setOnlineUserId(userId);
        return ctmMapper.notInterestedPage(page, ctm);
    }
}
